package com.example.hookapp;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;


import com.example.hookapp.base.HookerBase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class HookClick extends HookerBase {
    public static final String TAG = HookClick.class.getName();

    private static HookClick instance;

    private HookClick() {

    }

    public static HookClick getInstance() {
        if (instance == null) {
            synchronized (HookClick.class) {
                if (instance == null) {
                    instance = new HookClick();
                }
            }
        }
        return instance;
    }
    @Override
    protected String providePackageName() {
        return null;
    }

    @Override
    protected String[] providePackageNames() {
        return new String[]{"com.sankuai.meituan","com.syh.bigbrain","com.abxsss.app"};
    }

    @Override
    protected String provideMainActivityName() {
        return "com.aaxxsspp.app.MainActivity";
    }

    @Override
    protected void onApplicationAttach(Context context) {
        MyReceiver myReceiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("InterceptClick");
        intentFilter.addAction("CancelInterceptClick");
        context.registerReceiver(myReceiver,intentFilter);
    }

    @Override
    protected void onActivityCreate(Activity activity, XC_MethodHook.MethodHookParam param) throws Throwable {
        handler.postDelayed(() -> {
            try {
                ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
                FloatMainView floatMainView = new FloatMainView(activity, TAG);
                decorView.addView(floatMainView);
                sendLogMsg("进入-->" + activity.getClass().getName());
            } catch (Exception e) {
                log("添加View异常：" + e.getMessage());
            }
        }, 500);
    }

    private void log(String s) {
        Log.e(TAG, "======" + s);
    }

    HashMap<Object, View.OnClickListener> clickListenerHashMap = new HashMap<>();
    HashMap<Object, View.OnClickListener> clickListenerHashMap2 = new HashMap<>();

    List<String> hookClassList = new ArrayList<>();

    @Override
    protected void hookStart(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
//        findAndHookMethod(View.class.getName(), "setOnClickListener", View.OnClickListener.class, new XC_MethodHook() {
//            @Override
//            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
//                super.beforeHookedMethod(param);
//                Object listenerObj = param.args[0];
//                String className = listenerObj.getClass().getName();
//                if(!hookClassList.contains(className)){
//                    hookClassList.add(className);
//                    Log.e("===========","设置监听器：" + listenerObj);
//                    findAndHookMethod(listenerObj.getClass().getName(),"onClick",View.class,new XC_MethodHook(){
//                        @Override
//                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
//                            super.beforeHookedMethod(param);
//                            Log.e("===========","点击啦：" + param);
//                            toastLong("performClick点击啦：" + param.args[0] + "\n" + param.thisObject);
//                            try {
//                                throw new RuntimeException();
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
//                        }
//                    });
//                }
//            }
//        });
        findAndHookMethod(View.class.getName(), "setOnClickListener", View.OnClickListener.class, new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                super.beforeHookedMethod(param);
                if(param.args[0].toString().contains(FloatMainView.class.getName())){
                    return;
                }
                clickListenerHashMap.put(param.thisObject, (View.OnClickListener) param.args[0]);
                param.args[0] = new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        View.OnClickListener clickListener = clickListenerHashMap.get(view);
                        if(!clickListener.toString().contains(FloatMainView.class.getName())){
                            sendLogMsg("performClick点击啦：" + view + "\n监听器：" + clickListener);
                        }

                        if(!isInterceptClick && !clickListener.toString().contains(FloatMainView.class.getName())){
                            clickListener.onClick(view);
                        }
                        try {
                            throw new RuntimeException();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                };
            }
        });

//        findAndHookMethod(View.class.getName(), "performClick", new XC_MethodHook() {
//            @Override
//            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                super.afterHookedMethod(param);
//                View.OnClickListener onClickListener = clickListenerHashMap.get(param.thisObject);
//                if(!onClickListener.toString().contains(FloatMainView.class.getName())){
//                    sendLogMsg("performClick点击啦：" + param.thisObject + "\n监听器：" + onClickListener);
//                }
//                if (onClickListener != null && !isInterceptClick && !onClickListener.toString().contains(FloatMainView.class.getName())) {
//                    onClickListener.onClick((View) param.thisObject);
//                }
//                try {
//                    throw new RuntimeException();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//
//        });
    }

    private boolean isInterceptClick;

    class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
//            sendLogMsg("收到了廣播：" + intent.getAction());
            switch (intent.getAction()) {
                case "InterceptClick":
                    isInterceptClick = true;
                    break;
                case "CancelInterceptClick":
                    isInterceptClick = false;
                    break;
            }
        }
    }
}
